﻿using System;
using System.Web;
using PmfWeb.Core.Utility;

namespace PmfWeb.Core.Web
{
    ///<summary>
    /// cookies操作扩展类
    ///</summary>
    public static class Cookies
    {
        ///<summary>
        /// cookie域名
        ///</summary>
        public static string Domain = (0 == ConfigHelper.GetString("domain").Length ? Current.GetHost() : "");

        #region 保存cookie对象 public static void Save(HttpCookie cookie)
        /// <summary>
        /// 设置cookie
        /// </summary>
        /// <param name="cookieName">cookie名</param>
        /// <param name="cookieValue">cookie值</param>
        /// <param name="isEncode">是否编码</param>
        public static void Set(string cookieName, string cookieValue, bool isEncode)
        {
            HttpCookie myCookie = new HttpCookie(cookieName);
            if(isEncode)
            {
                myCookie.Value = HttpUtility.UrlEncode(cookieValue);
                myCookie.Value = myCookie.Value.Replace("+","%20");
            }
            else
            {
                myCookie.Value = cookieValue;
            }
            if (String.IsNullOrEmpty(Domain))
                myCookie.Domain = Domain;
            myCookie.Path = "/";
            HttpContext.Current.Response.Cookies.Add(myCookie);
        }
        public static string Get()
        {
            return Domain;
        }

        ///<summary>
        /// 设置cookie
        ///</summary>
        ///<param name="name">cookie名字</param>
        ///<param name="sValue">cookie值</param>
        public static void Set(string name, string sValue)
        {
            Set(name, sValue, true);
        }
        #endregion

        #region 获取指定cookie的值 public static string Get(string key)
        ///<summary>
        /// 获取客户端的cookie值
        ///</summary>
        ///<param name="key">cookie名字</param>
        ///<param name="isDecode">是否解码</param>
        ///<returns>cookie的值</returns>
        public static string Get(string key, bool isDecode)
        {
            HttpCookie cookie = HttpContext.Current.Request.Cookies[key];
            if (null == cookie) return "";
            if (isDecode)
            {
                string temp = HttpUtility.UrlDecode(cookie.Value);
                return temp.Replace("%20", " ");
            }
            return cookie.Value;
        }

        ///<summary>
        /// 获取cookie的值,默认需要解码的方式
        ///</summary>
        ///<param name="key">cookie名字</param>
        ///<returns>cookie的值</returns>
        public static string Get(string key)
        {
            return Get(key, true);
        }
        #endregion

        #region 删除指定的cookie public static void Remove(string key)
        /// <summary>
        /// 删除指定的cookie
        /// </summary>
        /// <param name="key">要删除的cookie名字</param>
        public static void Remove(string key)
        {
            Set(key, "", false);
        }
        #endregion
    }
}
